package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.RegionMapper;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.pojo.dto.PartnerQueryParam;
import com.heima.pojo.domain.PageResult;
import com.heima.pojo.domain.Region;

import com.heima.pojo.vo.RegionVo;
import com.heima.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public PageResult regionQueryPage(PartnerQueryParam pqp) {
        PageHelper.startPage(pqp.getPageIndex(),pqp.getPageSize());
        Long l = null;
        if (pqp.getId()!=null){
             l = Long.valueOf(pqp.getId());
        }
        Page<RegionVo> list = regionMapper.queryList(pqp.getName(),l);
        long total = list.getTotal();   //总记录数
        Long pages = (long) list.getPages();    //总页数
        List<RegionVo> result = list.getResult();
        Integer pageSize = list.getPageSize();  //每页大小
        Integer pageNum =  list.getPageNum();    //当前页码
        return new PageResult(pageNum,pageSize,pages,total,result);

       /* Page<Region> page = new Page<>(pqp.getPageIndex(), pqp.getPageSize());
        LambdaQueryWrapper<Region> like = Wrappers.<Region>lambdaQuery().like(pqp.getName() != null, Region::getName, pqp.getName())
                .eq(pqp.getId()!= null,Region::getId,pqp.getId());
         regionMapper.selectPage(page,like);
        return new PageResult<>(
                (int) page.getCurrent(),
                (int) page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords()
                );*/
    }

    @Override
    public Boolean insertRegion(Region region) {
        if (region.getName() == null){ return false;}

        int x = regionMapper.insert(region);
//        int x = regionMapper.insertRegion(name,remark);

        return x>0;
    }

    @Override
    public Boolean updateRegion(Region region,String id) {
//        int x = regionMapper.updateRegion(region.getId(),region.getName(),region.getRemark());
        region.setId(id);
        if (region.getId()==null){return false;}
        LambdaQueryWrapper<Region> eq = Wrappers.<Region>lambdaQuery().eq(Region::getId, region.getId());
        int x = regionMapper.update(region, eq);
        return x>0;
    }

    @Override
    public Boolean del(Long id) {
        int x = regionMapper.deleteById(id);
//        int x = regionMapper.del(id);
        return x>0;
    }
}
